package com.enjoyor.soa.traffic.restt.tpcms.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.enjoyor.soa.traffic.api.dto.tpcms.TemplateDto;
import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.frame.security.bean.dto.UserDto;
import com.enjoyor.soa.traffic.restt.tpcms.dubbo.ITemplateConsumeService;
import com.enjoyor.soa.traffic.restt.tpcms.dubbo.invoke.UbmsDicServiceInvoke;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Auth
@RestController
@RequestMapping({ "/api/template" })
@Api("TemplateController相关api")
@CrossOrigin
public class TemplateController {

    @Autowired
    private ITemplateConsumeService templateConsumeService;

    @Autowired
    private UbmsDicServiceInvoke ubmsDicServiceInvoke;

    @GetMapping(value = { "/select/{id}" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation(value = "根据hisId查询操作记录", notes = "查询数据库中某条操作记录")
    @ApiImplicitParam(name = "id", value = "id", defaultValue = "", paramType = "path", required = true, dataType = "String")
    public ResultPojo select(@PathVariable("id") String id, HttpServletRequest request) {
        return templateConsumeService.select(id);
    }

    @SuppressWarnings({ "rawtypes" })
    @GetMapping(value = { "/selectPage" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mapBean[timeStart]", value = "mapBean[timeStart]", defaultValue = "2018/4/23 13:00:00", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "mapBean[timeEnd]", value = "mapBean[timeEnd]", defaultValue = "2018/4/23 15:00:00", paramType = "query", dataType = "string") })
    public ResultPojo selectPage(Page page) {
        return templateConsumeService.selectPage(page);
    }

    @PostMapping(value = { "/create" }, consumes = { "application/json; charset=UTF-8" }, produces = {
            "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("创建数据")
    public ResultPojo create(@RequestBody TemplateDto template, HttpServletRequest request) {
        return templateConsumeService.insert(template, ((UserDto) request.getAttribute("currentUser")).getUserName());
    }

    @PutMapping(value = { "/update" }, consumes = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("更新数据")
    public ResultPojo update(@RequestBody TemplateDto template, HttpServletRequest request) {
        return templateConsumeService.edit(template, ((UserDto) request.getAttribute("currentUser")).getUserName());
    }

    @DeleteMapping(value = { "/delete/{id}" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("删除数据")
    @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true, dataType = "String")
    public ResultPojo delete(@PathVariable("id") String id, HttpServletRequest request) {
        return templateConsumeService.delete(id, ((UserDto) request.getAttribute("currentUser")).getUserName());
    }

    @GetMapping(value = { "/selectUser" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    public ResultPojo selectUser(HttpServletRequest request) {
        return new ResultPojo(((UserDto) request.getAttribute("currentUser")).getUserName());
    }

    @PostMapping(value = { "/checkToken" }, consumes = { "application/json; charset=UTF-8" }, produces = {
            "application/json; charset=UTF-8" })
    @ResponseBody
    public ResultPojo checkToken(@RequestParam String token, @RequestParam String sysKey) {
        return templateConsumeService.check(token, sysKey);
    }

    @GetMapping(value = { "/selectDic/{parentCode}" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation(value = "根据parentCode查询公共字典", notes = "查询ubms的查询公共字典数据")
    public ResultPojo selectDic(@PathVariable("parentCode") String parentCode, HttpServletRequest request) {
        return new ResultPojo(ubmsDicServiceInvoke.getPublicDic(parentCode));
    }

    @GetMapping(value = { "/selectDicOld" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation(value = "根据parentCode查询公共字典", notes = "查询ubms的查询公共字典数据")
    public ResultPojo selectDicOld(@RequestParam String parentCode, HttpServletRequest request) {
        return new ResultPojo(ubmsDicServiceInvoke.getPublicDic(parentCode));
    }
    
}
